﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DNEL.Extensions
{
    /// <summary>
    /// 
    /// </summary>
    public static class ByteExtensions
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="b"></param>
        /// <param name="BitNumber"></param>
        /// <returns></returns>
        public static byte SetBitTo1(this byte b, int BitNumber)
        {
            if (BitNumber < 0 || BitNumber > 7) throw new ArgumentException("BitNumber must be 0..7");
            return (byte)(b | (byte)(0x01 << BitNumber));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="b"></param>
        /// <param name="BitNumber"></param>
        /// <returns></returns>
        public static byte SetBitTo0(this byte b, int BitNumber)
        {
            if (BitNumber < 0 || BitNumber > 7) throw new ArgumentException("BitNumber must be 0..7");
            return (byte)(b & (byte)~(0x01 << BitNumber));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="b"></param>
        /// <returns></returns>
        public static byte Reverse(this byte b)
        {
            ushort u1 = (ushort)b;
            ushort u2 = (ushort)~u1;
            b = (byte)u2;
            return b;
        }
    }
}
